package tools.settings;

import java.awt.Component;
import java.io.File;
import java.util.Map;
import javax.swing.JOptionPane;
import modules.pathloss.LinkBudgetCalculator;
import modules.pathloss.PathLossAdapter;
import modules.pathloss.model.*;
import tools.settings.xml.XMLSettingsReader;

/**
 *
 * @author Lynne Vandousselaere
 */
public class VerifySettings {

    private String[] vsettings = {"plm",
        "technology"};
    private String[] vallowed = {"costhata,freespace,wilos",
        "gsm,umts"};
    private static VerifySettings vs;

    private VerifySettings() {
    }

    public static VerifySettings getInstance() {
        if (vs == null)
            vs = new VerifySettings();
        return vs;
    }

    public SettingsMessage doXMLcheck(File bestand) {
        XMLSettingsReader reader = new XMLSettingsReader();
        try {
            Map<String, Map<String, String>> map = reader.getSettings(bestand);

            for (int i = 0; i < vsettings.length; i++)
                if (map.get(vsettings[i]) == null)
                    return new SettingsMessage("No parameter for (\"" + vsettings[i] + "\") was found, this parameter is mandatory.\n" +
                            "Following settings are allowed: \"" + vallowed[i] + "\"",
                            "Error while verifying XML file", JOptionPane.ERROR_MESSAGE, false);

            String tech = map.get("technology").get("value");
            if (!tech.equalsIgnoreCase("gsm") && !tech.equalsIgnoreCase("umts"))
                return new SettingsMessage("Wrong parameter for \"technology\" was found. Allowed parameters are \"gsm\" and \"umts\"",
                        "Error while verifying XML file", JOptionPane.ERROR_MESSAGE, false);

            String plm = map.get("plm").get("value");
            String[] req = null;

            req = PathLossAdapter.getInstance().getRequiredSettings(plm);

            if (req == null)
                return new SettingsMessage("Wrong parameter for pathlossmodel (\"plm\") was found. Allowed parameters are \"costhate\", \"freespace\" and \"wilos\"",
                        "Error while verifying XML file", JOptionPane.ERROR_MESSAGE, false);

            for (int i = 0; i < req.length; i++)
                if (!map.containsKey(req[i]))
                    return new SettingsMessage("Parameter \"" + req[i] + "\" not found!! This is a required parameter for the selected path loss model (" + plm + ").\n Please provide it, or change your path loss model.",
                            "Error while verifying XML file", JOptionPane.ERROR_MESSAGE, false);

            req = new LinkBudgetCalculator().getRequiredSettingNames();


            for (int i = 0; i < req.length; i++)
                if (!map.containsKey(req[i]))
                    return new SettingsMessage("Parameter \"" + req[i] + "\" not found!! This is a required parameter for the Link Budget Calculator.\n Please provide it.",
                            "Error while verifying XML file", JOptionPane.ERROR_MESSAGE, false);

            // Nothing happened ! Everything OK...
            return new SettingsMessage("No errors encountered while verifying file. You can now use the file as input \n " +
                    "for the settings of the dimensioning tool or path loss models.\n\n" + bestand.toString(),
                    "Success!!", JOptionPane.INFORMATION_MESSAGE, true);
        } catch (Exception ex) {
            return new SettingsMessage("Exception occurred, not sure why. Please debug. \n\n" + ex.toString(),
                    "Error while verifying XML file", JOptionPane.ERROR_MESSAGE, false);
        }
    }

    public class SettingsMessage {

        private Object msg;
        private String title;
        private int messageType;
        private boolean verified;

        public SettingsMessage(Object msg, String title, int messageType, boolean verified) {
            this.msg = msg;
            this.title = title;
            this.messageType = messageType;
            this.verified = verified;
        }

        public void showMessageDialog(Component parent, boolean information_msg_on) {
            if (!information_msg_on && messageType == JOptionPane.INFORMATION_MESSAGE)
                return;
            JOptionPane.showMessageDialog(parent, msg, title, messageType);
        }

        public boolean getVerified() {
            return verified;
        }
    }
}
